﻿#include "UVideoPanel.h"
#include "CBeltCtrMgr.h"
UVideoPanel::UVideoPanel(QWidget *parent)
{
	ui.setupUi(this);
	m_bIsPlaying = false;
	m_pTimer = new QTimer(this);
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);	
	connect(m_pTimer, &QTimer::timeout, this, &UVideoPanel::slotDataRefresh);
	connect(ui.pushButtonVideoPlay, &QPushButton::clicked, this, &UVideoPanel::slot_btnClicked);
}

UVideoPanel::~UVideoPanel()
{

}

bool UVideoPanel::startPlayVideo()
{
	SCtrInfo cCtrInfo;
	cCtrInfo.m_enCtrCmd = enCPlay;
	cCtrInfo.m_pLabel = ui.label;
	cCtrInfo.m_hHwnd= (HWND)ui.label->winId();
	cCtrInfo.m_bCtrSync = false;
	bool bRet = m_pBeltCtrMgr->ctrDevice(cCtrInfo);
	if (bRet)
	{
		m_bIsPlaying = true;
		ui.pushButtonVideoPlay->setIcon(QIcon(":/Belt/Resource/stop.png"));
	}
	else
	{
		m_bIsPlaying = false;
	}
	return bRet;
}

bool UVideoPanel::endPlayVideo()
{
	SCtrInfo cCtrInfo;
	cCtrInfo.m_enCtrCmd = enCStopPlay;
	cCtrInfo.m_hHwnd = (HWND)ui.label->winId();
	cCtrInfo.m_bCtrSync = false;
	bool bRet = m_pBeltCtrMgr->ctrDevice(cCtrInfo);
	if (bRet)
	{
		m_bIsPlaying = true;
		ui.pushButtonVideoPlay->setIcon(QIcon(":/Belt/Resource/run.png"));
	}
	else
	{
		m_bIsPlaying = false;
	}
	return bRet;
}

void UVideoPanel::slot_btnClicked()
{
	if (m_bIsPlaying)
	{
		endPlayVideo();
		m_bIsPlaying = false;
	}
	else
	{
		startPlayVideo();
		m_bIsPlaying = true;
	}
}

void UVideoPanel::refreshData(bool bRefresh)
{
	if (bRefresh != m_bIsPlaying)
	{
		if (bRefresh)
		{
			startPlayVideo();
			m_bIsPlaying = true;
		}
		else
		{
			endPlayVideo();
			m_bIsPlaying = false;
		}
	}
}

void UVideoPanel::slotDataRefresh()
{

}
